//
//  ViewController.m
//  dogBook
//
//  Created by 黄辉 on 16/12/8.
//  Copyright © 2016年 黄辉. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

static int cnt ;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"狗书书屋";
    
    NSString *path = @"/Users/huanghui/Desktop/dog-dick-book/dogBook/image/book";
    NSFileManager *fm = [NSFileManager defaultManager];
    dataArray = [fm contentsOfDirectoryAtPath:path error:nil];
    mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:mainScrollView];
    mainScrollView.delegate = self;
    
    mainScrollView.bounces = NO;
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor grayColor];
    [mainScrollView addSubview:_tableView];
    _tableView.bounces = NO;
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height/3-21;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DogBookShell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        cell = [[DogBookShell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ID"];
    }
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    for (int i = 0; i < 3; i++) {
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(10 + ((self.view.frame.size.width-40) / 3 + 10) * i, 4, (self.view.frame.size.width - 40) / 3, (self.view.frame.size.height - 180) / 3);
        button.tag = cnt;//将cnt设置成button的tag，方便获取
        [button setImage:[UIImage imageNamed:[dataArray objectAtIndex:cnt++]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:button];
        
    }
    
    return cell;
}

- (void)onClick:(UIButton *)sender{
    LookBooks * readBook = [[LookBooks alloc] init];
    NSString *path2 = @"/Users/huanghui/Desktop/BookShelf/BookShelf/rao";
    NSFileManager * fm = [NSFileManager defaultManager];
    _array = [fm  contentsOfDirectoryAtPath:path2 error:nil];
    switch (sender.tag) {
        case 0:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:0]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 1:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:1]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 2:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:2]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 3:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:3]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 4:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:4]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 5:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:5]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 6:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:6]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 7:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:7]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 8:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:8]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 9:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:9]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 10:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:10]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 11:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:11]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 12:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:12]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 13:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:13]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 14:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:14]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 15:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:15]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 16:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:16]] encoding:NSUTF8StringEncoding error:nil];
            break;
        case 17:
            readBook.str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForAuxiliaryExecutable:[_array objectAtIndex:17]] encoding:NSUTF8StringEncoding error:nil];
            break;
            
            
        default:
            break;
    }
    //设置翻转模式
    [readBook setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:readBook animated:nil completion:nil];
    //[self.navigationController pushViewController:readBook animated:YES];
    return;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
